package com.vijayan.profile;

import java.io.IOException;
import java.util.Date;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.vijayan.profile.constant.ProfileConstants;
import com.vijayan.profile.model.Profile;
import com.vijayan.profile.service.ProfileLocalServiceUtil;
import com.vijayan.profile.util.ProfileUtil;

/**
 * Author: vcvijayan
 * Email Address: c.vijayan.mca@gmail.com
 */
public class ProfileController extends VCVPortlet {

	@Override
	public void doView(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		// TODO Auto-generated method stub
		super.doView(renderRequest, renderResponse);
	}

	@Override
	public void processAction(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		// TODO Auto-generated method stub
		String action = ParamUtil.getString(actionRequest, ProfileConstants.ACTION);
		if(action.equalsIgnoreCase(ProfileConstants.EDIT_PROFILE)){
			editProfile(actionRequest, actionResponse, ProfileConstants.ACTION);
		} else if(action.equalsIgnoreCase(ProfileConstants.DELETE_PROFILE)){
			deleteProfile(actionRequest, actionResponse, ProfileConstants.ACTION);
		} else if(action.equalsIgnoreCase(ProfileConstants.SEARCH_PROFILE)){
			searchProfile(actionRequest, actionResponse, ProfileConstants.ACTION);
		} 
		super.processAction(actionRequest, actionResponse);
	}

	@Override
	public void serveResource(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws IOException,
			PortletException {
		// TODO Auto-generated method stub
		super.serveResource(resourceRequest, resourceResponse);
	}
 
	private void editProfile(ActionRequest actionRequest,
			ActionResponse actionResponse, String action)throws IOException, PortletException {
		String profileName = ParamUtil.getString(actionRequest, ProfileConstants.PROFILE_NAME);
		ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		Profile profile = ProfileLocalServiceUtil.createProfileObj();
		profile.setProfileName(profileName);
		profile.setDateAdded(new Date());
		profile.setDateModified(new Date());
		profile.setAddedBy(themeDisplay.getUserId());
		profile.setModifiedBy(themeDisplay.getUserId());
		profile.setCompanyId(themeDisplay.getCompanyId());
		profile.setGroupId(themeDisplay.getScopeGroupId());
		try {
			profile.setProfileCode(ProfileUtil.getProfileCode(profile));
			profile = ProfileLocalServiceUtil.addProfile(profile);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void deleteProfile(ActionRequest actionRequest,
			ActionResponse actionResponse, String action)throws IOException, PortletException {
		long profileId = ParamUtil.getLong(actionRequest, ProfileConstants.PROFILE_ID);
		if(Validator.isNotNull(profileId)){
			try {
				ProfileLocalServiceUtil.deleteProfile(profileId);
			} catch (PortalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private void searchProfile(ActionRequest actionRequest,
			ActionResponse actionResponse, String action)throws IOException, PortletException {
		
	}

}
